package com.zjm.find.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjm.find.pojo.Moment;
import com.zjm.find.pojo.Strategy;
import com.zjm.find.pojo.UserCollection;
import com.zjm.find.service.MomentService;
import com.zjm.find.utils.JSONResult;
import com.zjm.find.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RequestMapping("/moment")
@RestController
public class MomentController {

    @Autowired
    private MomentService momentService;

    @PostMapping("/createMoment")
    public JSONResult createMoment(String mid, String description, int uid) throws ParseException {
        Moment moment = new Moment();
        moment.setMid(mid);
        moment.setUid(uid);
        moment.setDescription(description);
        moment.setCreateTime(TimeUtils.getDateTime());
        momentService.createMoment(moment);
        return JSONResult.successMsg("发布成功");
    }

    @PostMapping("/createStrategy")
    public JSONResult createStrategy(Strategy strategy) throws ParseException {
        strategy.setCreateTime(TimeUtils.getDateTime());
        strategy.setType(0);
        if (momentService.createStrategy(strategy)) {
            return JSONResult.successMsg("发布成功");
        }
        return JSONResult.successMsg("发布失败");
    }

    @GetMapping("/getMoment")
    public JSONResult getMoment(Integer uid,
                                @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Moment> momentPageInfo = new PageInfo(momentService.getMoment(uid));
        return JSONResult.msgAndData("查询成功", momentPageInfo);
    }

    @PostMapping("/deleteMoment")
    public JSONResult deleteMoment(String mid) {
        if (momentService.deleteMoment(mid)) {
            return JSONResult.successMsg("删除成功");
        }
        return JSONResult.successMsg("删除失败");
    }

    @GetMapping("/getStrategy")
    public JSONResult getStrategy(Strategy strategy,
                                  @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Strategy> momentPageInfo = new PageInfo(momentService.getStrategy(strategy));
        return JSONResult.msgAndData("查询成功", momentPageInfo);

    }

    @PostMapping("/updateStrategy")
    public JSONResult updateStrategy(Strategy strategy) throws ParseException {
        strategy.setUpdateTime(TimeUtils.getDateTime());
        if (momentService.updateStrategy(strategy)) {
            return JSONResult.successMsg("更新成功");
        } else {
            return JSONResult.errorMsg("更新失败");
        }
    }

    @PostMapping("/deleteStrategy")
    public JSONResult deleteStrategy(Strategy strategy) throws ParseException {
        strategy.setUpdateTime(TimeUtils.getDateTime());
        if (momentService.deleteStrategy(strategy)) {
            return JSONResult.successMsg("删除成功");
        } else {
            return JSONResult.errorMsg("删除失败");
        }
    }


    @PostMapping("/createCollection")
    public JSONResult createCollection(UserCollection userCollection) {
        if (momentService.createCollection(userCollection)) {
            return JSONResult.successMsg("收藏成功");
        } else {
            return JSONResult.errorMsg("收藏失败");
        }
    }

    @PostMapping("/cancelCollection")
    public JSONResult cancelCollection(UserCollection userCollection) {
        if (momentService.cancelCollection(userCollection)) {
            return JSONResult.successMsg("取消收藏成功");
        } else {
            return JSONResult.errorMsg("取消收藏失败");
        }
    }

    @GetMapping("/selectCollection")
    public JSONResult selectCollection(UserCollection userCollection,
                                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                       @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Moment> momentPageInfo = new PageInfo(momentService.selectCollection(userCollection));
        return JSONResult.msgAndData("查询成功", momentPageInfo);
    }

    @GetMapping("/selectUserCollection")
    public JSONResult selectUserCollection(Strategy strategy,
                                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                           @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Strategy> momentPageInfo = new PageInfo(momentService.selectUserCollection(strategy));
        return JSONResult.msgAndData("查询成功", momentPageInfo);

    }

    @GetMapping("/selectUserHistory")
    public JSONResult selectUserHistory(Strategy strategy,
                                        @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Strategy> momentPageInfo = new PageInfo(momentService.selectUserHistory(strategy));
        return JSONResult.msgAndData("查询成功", momentPageInfo);
    }

    @GetMapping("/getTaps")
    public JSONResult getTaps(){
        return JSONResult.msgAndData("查询成功", momentService.getTabs());
    }
}
